//
//  PrayDetailView.m
//  Azkar
//
//  Created by 王易 on 2018/8/26.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "PrayDetailView.h"

/**
 祈福详情View
 */
@interface PrayDetailView()

@property (nonatomic,weak) UILabel *title;

@property (nonatomic,weak) MyLinearLayout *rootLayout;

@property (nonatomic,weak) UIImageView *iconV;

@property (nonatomic,weak) UILabel *userName;

@property (nonatomic,weak) UILabel *desc;

@property (nonatomic,weak) UIImageView *imageV;

@property (nonatomic,weak) UILabel *message;

@property (nonatomic,weak)  UILabel *prayer;

@property (nonatomic,weak) UIImageView *prayerImage;

@property (nonatomic,weak) MyRelativeLayout *prayerLayout;
@end
@implementation PrayDetailView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)initWithUI
{
    self.backgroundColor = Color(@"#F7F7F7");
    self.wrapContentHeight = YES;
    self.myHorzMargin = 0;
    
    MyLinearLayout *rootLayout = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    rootLayout.backgroundColor = [UIColor whiteColor];
    self.rootLayout = rootLayout;
    rootLayout.wrapContentHeight = YES;
    rootLayout.myHorzMargin = 0;
    rootLayout.padding = UIEdgeInsetsMake(5, 12, 16, 12);
    [self addSubview:rootLayout];
    
    UILabel *title = [UILabel new];
    self.title = title;
    title.myHorzMargin = 0;
    title.wrapContentHeight = YES;
    title.textColor = Color(@"002812");
    title.font = FontSize(20);
    [rootLayout addSubview:title];
    
    [self addMoreMessage:rootLayout];
    
    MyLinearLayout *imageLayout = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    imageLayout.myHorzMargin = 0;
    imageLayout.myTop = 15;
    imageLayout.wrapContentHeight = YES;
    imageLayout.subviewVSpace = 12;
    [rootLayout addSubview:imageLayout];
    
    UIImageView *imageV = [UIImageView new];
    self.imageV = imageV;
    imageV.myHorzMargin = 0;
    imageV.myHeight = [MyDimeScale scaleH:175];
    [imageLayout addSubview:imageV];
    
    UILabel *message = [UILabel new];
    self.message = message;
    message.myHorzMargin = 0;
    message.wrapContentHeight = YES;
    message.textColor = Color(@"#6F6F6F");
    message.font = FontSize(15);
    [imageLayout addSubview:message];
    
    [self addBottomLayout:rootLayout];
    
    MyRelativeLayout *commentsLayout = [MyRelativeLayout new];
    commentsLayout.myHorzMargin = 0;
    commentsLayout.myTop = 10;
    commentsLayout.padding = UIEdgeInsetsMake(12, 12, 0, 12);
    commentsLayout.wrapContentHeight = YES;
    commentsLayout.backgroundColor = [UIColor whiteColor];
    [self addSubview:commentsLayout];
    
    UILabel *comments = [UILabel new];
    comments.text = TLString(@"greeting_cards_comments");
    comments.wrapContentSize = YES;
    comments.font = FontSize(20);
    comments.textColor = Color(@"#002812");
    [commentsLayout addSubview:comments];
}
- (void)addMoreMessage:(MyBaseLayout *)baseLayout
{
    MyRelativeLayout *relativeLayout = [MyRelativeLayout new];
    relativeLayout.myHorzMargin = 0;
    relativeLayout.myTop = 15;
    relativeLayout.wrapContentHeight = YES;
    [baseLayout addSubview:relativeLayout];
    
    UIImageView *iconV = [UIImageView new];
    self.iconV = iconV;
    iconV.layer.cornerRadius = 17;
    iconV.layer.masksToBounds = YES;
    iconV.myLeading = iconV.myTop = 0;
    iconV.myWidth = iconV.myHeight = 34;
    [relativeLayout addSubview:iconV];
    
    UILabel *userName = [UILabel new];
    self.userName = userName;
    userName.wrapContentSize = YES;
    userName.leadingPos.equalTo(iconV.trailingPos).offset(8);
    userName.textColor = Color(@"#00732F");
    userName.font = FontSize(13);
    userName.myTop = 0;
    [relativeLayout addSubview:userName];
    
    UILabel *desc = [UILabel new];
    self.desc = desc;
    desc.leadingPos.equalTo(userName.leadingPos);
    desc.myTrailing = 0;
    desc.topPos.equalTo(userName.bottomPos).offset(3);
    desc.font = FontSize(11);
    desc.textColor = Color(@"#929292");
    desc.wrapContentHeight = YES;
    [relativeLayout addSubview:desc];
    
}
- (void)addBottomLayout:(MyBaseLayout *)baseLayout
{
    MyLinearLayout *linearLayout = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
    linearLayout.myHorzMargin = 0;
    linearLayout.myTop = 28;
    linearLayout.subviewHSpace = 24;
    linearLayout.gravity = MyGravity_Horz_Fill;
    linearLayout.wrapContentHeight = YES;
    [baseLayout addSubview:linearLayout];
    
    MyRelativeLayout *leadingLayout = [MyRelativeLayout new];
    self.prayerLayout = leadingLayout;
    leadingLayout.myLeading = leadingLayout.myTop =  0;
//    leadingLayout.weight = 0.5;
    leadingLayout.myHeight = 34;
    leadingLayout.layer.cornerRadius = 17;
    leadingLayout.layer.masksToBounds = YES;
    leadingLayout.backgroundColor = Color(@"#2B9958");
    [linearLayout addSubview:leadingLayout];
    WeakSelf
    [leadingLayout hyb_addTapGestureWithCallback:^(UITapGestureRecognizer *sender) {
        NSString *url;
        StrongSelf
        BOOL like;
        if (self.blessModel) {
        if (!self.blessModel.ID) {
            [AlertTools showMessage:TLString(@"error")];
            return;
        }
            url = [NSString stringWithFormat:@"%@/%@/like",BlessBlog,self.blessModel.ID];
            like = self.blessModel.like;
        }else{
            if (!self.publicModel.ID) {
                [AlertTools showMessage:TLString(@"error")];
                return;
            }
            url = [NSString stringWithFormat:@"%@/%@/like",PublicBless,self.publicModel.ID];
            like = self.publicModel.like;
        }
        
        [AlertTools showLoading];
        if(!like){
            
            [[AzkarApi postData:url withParm:nil]subscribeNext:^(id x) {
                [AlertTools hideLoading];
                if (self.blessModel) {
                    self.blessModel.like = YES;
                    self.blessModel.cnt_like = [NSString stringWithFormat:@"%@",x];
                }else{
                    self.publicModel.like = YES;
                    self.publicModel.cnt_like = [NSString stringWithFormat:@"%@",x];
                }
                self.blessModel = self.blessModel;
                self.publicModel = self.publicModel;
                [[NSNotificationCenter defaultCenter]postNotificationName:AzkarComments_Const object:nil];
            } error:^(NSError *error) {
//                [AlertTools hideLoading];
            }];
        }else{
            
            [[AzkarApi deleteData:url withParm:nil]subscribeNext:^(id x) {
                [AlertTools hideLoading];
                if (self.blessModel) {
                    self.blessModel.like = NO;
                    self.blessModel.cnt_like = [NSString stringWithFormat:@"%@",x];
                }else{
                    self.publicModel.like = NO;
                    self.publicModel.cnt_like = [NSString stringWithFormat:@"%@",x];
                }
                self.blessModel = self.blessModel;
                self.publicModel = self.publicModel;
                [[NSNotificationCenter defaultCenter]postNotificationName:AzkarComments_Const object:nil];
            } error:^(NSError *error) {
//                [AlertTools hideLoading];
            }];
            
        }

    }];
    
    UILabel *prayer = [UILabel new];
    self.prayer = prayer;
    prayer.textColor = Color(@"#FFFFFF");
    prayer.font = FontSize(12);
    prayer.myCenterX = 12;
    prayer.myCenterY = 0;
    prayer.wrapContentSize = YES;
    [leadingLayout addSubview:prayer];
    
    UIImageView *leadImage = [UIImageView new];
    self.prayerImage = leadImage;
    leadImage.image = Image(@"dua_ic_s");
    leadImage.myWidth = leadImage.myHeight = 19;
    leadImage.trailingPos.equalTo(prayer.leadingPos).offset(6);
    leadImage.myCenterY = 0;
    [leadingLayout addSubview:leadImage];
    
    MyRelativeLayout *trailingLayout = [MyRelativeLayout new];
//    trailingLayout.weight = 1.0;
    trailingLayout.myHeight = 34;
    trailingLayout.layer.cornerRadius = 17;
    trailingLayout.layer.borderWidth = 1;
    trailingLayout.layer.borderColor = Color(@"#D6D6D6").CGColor;
    trailingLayout.layer.masksToBounds = YES;
     trailingLayout.myTop = 0;
    trailingLayout.myLeading = 0;
    [linearLayout addSubview:trailingLayout];
    
    UILabel *shareTitle = [UILabel new];
    shareTitle.text = TLString(@"greeting_cards_share");
    shareTitle.textColor = Color(@"#525252");
    shareTitle.font = FontSize(12);
    shareTitle.wrapContentSize = YES;
    shareTitle.myCenterX = 12;
    shareTitle.myCenterY = 0;
    [trailingLayout addSubview:shareTitle];
    
    UIImageView *shareImage = [UIImageView new];
    shareImage.image = Image(@"share_gray_ic");
    shareImage.myCenterY = 0;
    shareImage.trailingPos.equalTo(shareTitle.leadingPos).offset(6);
    shareImage.myWidth = 18;
    shareImage.myHeight = 16;
    [trailingLayout addSubview:shareImage];
    
    [trailingLayout hyb_addTapGestureWithCallback:^(UITapGestureRecognizer *sender) {
       
        StrongSelf
        NSString *lang_type = self.blessModel.lang_type;
        BOOL isPublic = NO;
        NSString *Id = self.blessModel.ID;
        if (self.publicModel) {
            lang_type = self.publicModel.lang_type;
            isPublic = YES;
            Id = self.publicModel.ID;
        }
        ShareView *shareView = [ShareView new];
        shareView.lang_type = lang_type;
        shareView.isPublic = isPublic;
        shareView.pbModel = self.publicModel;
        shareView.model = self.blessModel;
        shareView.Id = Id;
        shareView.block = ^{
            if (self.blessModel) {
                self.blessModel.cnt_shared = [NSString stringWithFormat:@"%ld",self.blessModel.cnt_shared.integerValue+1];
            }
            else if (self.publicModel){
                self.publicModel.cnt_shared = self.publicModel.cnt_shared+1;
            }
        };
        shareView.isReport = NO;
        [shareView showView];
        
    }];
    
}
- (void)setBlessModel:(BlessModel *)blessModel
{
    if (!blessModel) {
        return;
    }
    _blessModel = blessModel;

    self.title.myVisibility = MyVisibility_Gone;
    self.imageV.myVisibility = MyVisibility_Gone;
    if (blessModel.like) {
        self.prayer.text = [NSString stringWithFormat:@"%@ %@",blessModel.cnt_like,TLString(@"greeting_cards_azkar_Ameen_ya_rab")];
        self.prayer.textColor = Color(@"#929292");
        self.prayerLayout.backgroundColor = Color(@"#EFEFEF");
        self.prayerImage.image = Image(@"dua_ic_s");
    }else{
        
        
        self.prayer.textColor = Color(@"#FFFFFF");
        self.prayerLayout.backgroundColor = Color(@"#2B9958");
        self.prayer.text = [NSString stringWithFormat:@"%@ %@",blessModel.cnt_like,TLString(@"greeting_cards_azkar_Duas_for_this")];
        self.prayerImage.image = Image(@"dua_ic");
    }
    
    self.title.textAlignment = [MyBaseLayout isRTL] ? NSTextAlignmentRight : NSTextAlignmentLeft;
    [self.iconV sd_setImageWithURL:[NSURL URLWithString:blessModel.avatar] placeholderImage:Image(@"no_photo")];
    
    self.userName.text = blessModel.uname.length?blessModel.uname:TLString(@"greeting_cards_Anonymously");
    self.userName.textColor  =Color(@"#525252");
    [self.userName sizeToFit];
    
    self.message.text = blessModel.content;
    self.message.textAlignment = [MyBaseLayout isRTL] ? NSTextAlignmentRight : NSTextAlignmentLeft;
    [self.message sizeToFit];
    
    NSString *time = [DateHelper dateStrFromTimeStamp:blessModel.create_date dateFormat:@"dd/MM HH:mm"];
    self.desc.text = [NSString stringWithFormat:@"%@ %@    %ld %@",time,TLString(@"greeting_cards_azkar_released"),blessModel.cnt_read,TLString(@"greeting_cards_azkar_people_readed")];
    
    if ([[LanguageManager defaultManager].appLanguage isEqualToString:@"ar"]) {
        self.desc.text = self.desc.text.arabicString;
        self.prayer.text = self.prayer.text.arabicString;
    }
    [self.desc sizeToFit];
}
- (void)setPublicModel:(Public_BlessModel *)publicModel
{
    if (!publicModel) {
        return;
    }
    _publicModel = publicModel;
    
    self.title.myVisibility = MyVisibility_Visible;
    self.imageV.myVisibility = MyVisibility_Visible;
    
    if (!publicModel.like) {
        self.prayer.textColor = Color(@"#FFFFFF");
        self.prayerLayout.backgroundColor = Color(@"#2B9958");
        self.prayer.text = [NSString stringWithFormat:@"%@ %@",publicModel.cnt_like,TLString(@"greeting_cards_azkar_Duas_for_this")];
        self.prayerImage.image = Image(@"dua_ic");
    }else{
        self.prayer.text = [NSString stringWithFormat:@"%@ %@",publicModel.cnt_like,TLString(@"greeting_cards_azkar_Ameen_ya_rab")];
        self.prayer.textColor = Color(@"#929292");
        self.prayerLayout.backgroundColor = Color(@"#EFEFEF");
        self.prayerImage.image = Image(@"dua_ic_s");
    }
    
    self.title.textAlignment = [MyBaseLayout isRTL] ? NSTextAlignmentRight : NSTextAlignmentLeft;
    self.iconV.image = Image(@"icon90");
    
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:publicModel.bless_background] placeholderImage:NORMALIMGE];
    
    self.userName.text = TLString(@"iMuslim");
    self.userName.textColor  =Color(@"#00732F");
    [self.userName sizeToFit];
    
    self.title.text = publicModel.title;
    [self.title sizeToFit];
    
    self.message.text = publicModel.content;
    self.message.textAlignment = [MyBaseLayout isRTL] ? NSTextAlignmentRight : NSTextAlignmentLeft;
    [self.message sizeToFit];
    
    NSString *time = [DateHelper dateStrFromTimeStamp:publicModel.create_date dateFormat:@"dd/MM HH:mm"];
    self.desc.text = [NSString stringWithFormat:@"%@ %@    %ld %@",time,TLString(@"greeting_cards_azkar_released"),publicModel.cnt_read,TLString(@"greeting_cards_azkar_people_readed")];
    if ([[LanguageManager defaultManager].appLanguage isEqualToString:@"ar"]) {
        self.desc.text = self.desc.text.arabicString;
        self.prayer.text = self.prayer.text.arabicString;
    }
}
@end
